/**
 * 
 */
package map;


import java.util.*;
import java.util.Collection;
import java.util.Date;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedList;

import javax.swing.JPanel;

import main.Controller;

import processing.core.PApplet;

/**
 * @author Mathias
 *
 */
public class Map {
	
	private final boolean drawArrow = false;
	
	private Scale scale;
	
	private PApplet applet;
	private double zoomFactor = 0.5;
	public HashMap<Integer, Station> stations;
	
	public HashMap<String, Train> trains;

	
	
	public int getTime(Station s, Station g){
		int time = -1;
		Calendar date = Calendar.getInstance();
		int currTime = date.get(Calendar.HOUR_OF_DAY)*60 + date.get(Calendar.MINUTE);
		System.out.println("current time: "+currTime);
		
		LinkedList<Train> t = getTrains(s, currTime);
		for (Train tr : t){
			for (Waypoint w : tr.stations){
				if (w.station == g.getId()){
					time = w.arrival;
					break;
				}
				
			}
			if (time > -1) break;
		}
		  
		
		return Math.max(time - currTime, -1);
		
	}
	
	public void restore(){
		stations=Scale.reset();
	}
	
	boolean normal = false;
	public void changeView(){
		normal = !normal;
		scale.changeView(normal, stations);
	}
	
	public LinkedList<Train> getTrains(Station s, int currTime){
		LinkedList<Train> match = new LinkedList<Train>();
		for (Train t : trains.values()){
			for (Waypoint w : t.stations){
				if (w.station == s.getId() && w.departure >= currTime){
					match.add(t);
					System.out.println("found train with departure at start station: "+w.departure);
					break;
				}
				
			}
		}
		if (match.size() == 0) System.out.println("no train found for start station");
		return match;
	}
	
	public Map(HashMap<Integer, Station> stations, HashMap<String, Train> trains, PApplet applet){
		this.applet=applet;
		this.stations = stations;
		this.trains = trains;
		scale=new Scale(trains);
	}

	

	public void addStation(Station s){
		this.stations.put(s.getId(), s);
	}

	public void removeStation(int id){
		this.stations.remove(id);
	}

	public void setZoomFactor(double f){
		this.zoomFactor = f;
	}

	public void zoomIn(){
		this.zoomFactor = Math.min(1.0, this.zoomFactor + 0.1);
	}

	public void zoomOut(){
		this.zoomFactor = Math.max(0.0, this.zoomFactor - 0.1);
	}
	
	//Liste der Bahnhoefe die angezeigt werden
	//private HashMap<Integer, Station> list= new HashMap<Integer, Station>();
	public void initList(HashMap<Integer, Station> stationen){
		stations=stationen;
	}
	
	public HashMap<Integer, Station> getStation(){
		return stations;
	}

	Station main=null;
	public void searchForMainStation(String s){
		stations=Scale.stations;
		
		main=null;
		for (Station st: stations.values()){
			if (st.getName().equals(s)){
				main=st;
				st.setActive(true, applet) ;
			}
			else{
				st.setActive(false, applet);
			}
		}
			
		
		if (main==null){
			return;
		}
		
		stations = scale.findStations(main.getId());
	}
	
	

	public void draw(PApplet applet){ 
		for (Station s: this.stations.values()){		
				//if ((main!=null) && drawArrow && (!s.equals(main))){
				//	arrow(main.getLongitudeScaled(), main.getLatitudeScaled(), s.getLongitudeScaled(), s.getLatitudeScaled());
				//}
			
				s.draw(applet, zoomFactor);
		}

	}
	
	public void arrow(float x1, float y1, float x2, float y2) {
		applet.line(x1, y1, x2, y2);
		applet.pushMatrix();
		applet.translate(x2, y2);
		float a = applet.atan2(x1-x2, y2-y1);
		applet.rotate(a);
		applet.line(0, 0, -10, -10);
		applet.line(0, 0, 10, -10);
		applet.popMatrix();
		}

}
